Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add function to return a Certificate from DER format #293

Closed
wants to merge 18 commits into from

Conversation

jiangshaoqi
Copy link

add "from_der" function in the Certificate implementation. This function can create a Certificate struct from DER encoded certificate.

Make sure the certificate match the format of x509-parser in rcgen, or the generated Certificate will be different.
A safe way of this usage is to load the DER certificate generated by rcgen itself.

@cpu
Copy link
Member

cpu commented Oct 24, 2024

Hi @jiangshaoqi, thanks for the contribution.

Can you speak more to your motivation for wanting this?

You can already construct CertificateParams directly from DER with the x509-parser feature enabled. It's not clear to me why you would want a Certificate from the DER given this gives you few capabilities beyond accessing the DER/PEM encoding (which you basically already have in-hand), or the CertificateParams which you can construct from the DER already. Am I overlooking a use-case?

@jiangshaoqi
Copy link
Author

Hello @cpu , thank you for the reply.
I used this function in my project in the scenario: I am making a ResolvesServerCert to generate the fake end-entity certificates for my browser. I already have my own self-signed CA certificate, and have added it into browser's CA list.

In ResolvesServerCert, it requires the Certificate of my CA certificate, which has already been self signed and i need to directly load it as Certificate. If i read the certificate into CertificateParams, i will need to self_signed to generate the certificate.

Also, In my case, if i sign it again to get the Certificate, there will be some difference between my original certificate, because of x509 parser. for example, in my certificate:
X509v3 Basic Constraints: critical
CA: TRUE
the the new certificate from CertificateParams will automatically set CA: FALSE. (because of x509-parser implementation).

I also see people asking for: [https://github.com//issues/274]

What i did work for me, and i think it might be good for others. From your reply, i think i should also try use rcgen generate a CA certificate DER, get its CertificateParams, do self_signed on it, and check if the new self-signed certificate is the same as the original CA certificate.

If it is same, please ignore my request

@jiangshaoqi
Copy link
Author

I just checked: use rcgen generate a CA certificate DER, get its CertificateParams, do self_signed on it, and check if the new self-signed certificate is the same as the original CA certificate.

It also works in my scenario, please ignore my request and have a good day!

@oscartbeaumont
Copy link

oscartbeaumont commented Nov 13, 2024

@jiangshaoqi I would be curious how you achieved the same certificate because I can't seem to reproduce it myself.

When I run the following code and the assertion fails:

let CertifiedKey { cert, key_pair } = generate_simple_self_signed(vec!["abc".into()]).unwrap();

let before = cert.pem();
println!("{:?}", cert.pem());
println!("{:?}\n\n\n", key_pair.serialize_pem());

let key_pair = rcgen::KeyPair::from_pem(&key_pair.serialize_pem()).unwrap();
let cert = CertificateParams::from_ca_cert_der(&cert.try_into().unwrap())
    .unwrap()
    .self_signed(&key_pair)
    .unwrap();

println!("{:?}", cert.pem());
println!("{:?}\n\n\n", key_pair.serialize_pem());

assert_eq!(before, cert.pem());

I kinda feel like this PR should be reopened and merged as a stop gap solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants